<?php

namespace App\Http\Controllers\SystemManagement;

use App\Http\Controllers\IndexController;
use App\Models\Constant\Errors;
use App\Models\Error;
use App\Models\SystemManagement\OperationLog;
use Illuminate\Http\Request;

/**
 * Class LogController
 * @package App\Http\Controllers\SystemManagement
 * @group 日志管理
 */
class LogController extends IndexController
{
    /**
     * 日志列表
     * @return \Illuminate\Http\JsonResponse
     *
     * @queryParam id 日志id
     * @queryParam operator 操作者
     * @queryParam operator_time 操作时间
     * @queryParam ip 来源ip
     * @queryParam record 日志记录
     *
     * @response 200 {"msg":"success","code":200,"data":{"current_page":1,"data":[{"id":1,"operator":"han","operator_time":"0000-00-00 00:00:00","ip":"","record":""}],"first_page_url":"http:\/\/zhihuihuanwei.com\/api\/system\/log?page=1","from":1,"last_page":1,"last_page_url":"http:\/\/zhihuihuanwei.com\/api\/system\/log?page=1","next_page_url":null,"path":"http:\/\/zhihuihuanwei.com\/api\/system\/log","per_page":15,"prev_page_url":null,"to":1,"total":1}}
     */
    public function getLog()
    {
        $per_page = request()->input('per_page');
        $data = OperationLog::getData($per_page);
        return response()->json(encodeMsg($data,200));
    }

    /**
     * 按 操作者/操作时间 查询日志
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @bodyParam operator string required 操作者
     * @bodyParam operator_time string required 操作时间
     *
     * @queryParam id 日志id
     * @queryParam operator 操作者
     * @queryParam operator_time 操作时间
     * @queryParam ip 来源ip
     * @queryParam record 日志记录
     *
     * @response 200 {"code":200,"msg":"Success","data":[{"id":1,"operator":"han","operator_time":"0000-00-00 00:00:00","ip":"","record":""}]}
     * @response 400 {"code":200,"msg":"param error","data":"搜索条件不符"}
     * @response 404 {"code":200,"msg":"not found","data":""您输入的关键词不存在，请重新输入！}
     */
    public function seekLog(Request $request){
        $operator = $request->input('operator');
        $time = $request->input('operator_time');
        $err = new Error();
        if(is_null($operator) && is_null($time)){
            $err->setError(Errors::FAILED);
            $err->setData('搜索条件不符');
            return response()->json($err);
        }
        $data = OperationLog::getDataByCondition($operator,$time);
        if($data->isEmpty()){
            $err->setError(Errors::NOT_FOUND);
            $err->setData('您输入的关键词不存在，请重新输入！');
        }else{
            $err->setData($data);
        }
        return response()->json($err);
    }

    /**
     * 批量清除日志
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam ids array required 日志id
     *
     * @response 204 {"code":204,"msg":"deleted","data":"删除成功"}
     */
    public function delLog(Request $request){
        $ids = $request->input('ids');
        foreach ($ids as $k=>$id){
            $ids[$k] = (int) $id;
        }
        $data = OperationLog::batchDelData($ids);
        return response()->json($data,204);
    }

    /**
     * 按时间清除日志
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam clear_time string required 清除时间，条件定义clear_time 0：一周之前 1：一月之前 2：三月之前 3: 半年之前 4：一年之前
     *
     * @response 204 {"msg":"deleted","code":204,"data":"\u6e05\u9664\u6210\u529f"}
     */
    public function delLogByTime(Request $request){
        $time = $request->input('clear_time');
        switch ((int)$time){
            case 0:
                $clear = date("Y-m-d H:i:s", strtotime("-1 weeks"));
                break;
            case 1:
                $clear = date("Y-m-d H:i:s", strtotime("-1 months"));
                break;
            case 2:
                $clear = date("Y-m-d H:i:s", strtotime("-3 months"));
                break;
            case 3:
                $clear = date("Y-m-d H:i:s", strtotime("-6 months"));
                break;
            case 4:
                $clear = date("Y-m-d H:i:s", strtotime("-1 year"));
                break;
            default:
                break;
        }
        OperationLog::clean($clear);
        //OperationLog::writeData('清理'.$time.'日志');
        return response()->json(encodeMsg("清除成功",204));
    }

}
